package com.app.controller;

import com.app.exceotion.DevNoPermissonExeption;
import com.app.pojo.DevUser;
import com.app.service.DevService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/dev")
public class devloginController {
    /**
     * 跳转到开发者登录界面
     */
    @Resource
    private DevService devService;
    @RequestMapping("/login")
    public  String devlogin(){
        return "devlogin";
    }
    //退出
    @RequestMapping("/logout")
    public String logout(HttpServletRequest request){
        request.getSession().removeAttribute("UserSession");
        return "devlogin";
    }

    /**
     * 登录
     */
    @RequestMapping("/main")
    public String toMain(HttpSession session) throws DevNoPermissonExeption{
        DevUser devUserSession = (DevUser) session.getAttribute("UserSession");
        if(devUserSession == null){
            throw new DevNoPermissonExeption("对不起,您没有权限,请登录后访问");
        }
        return "developer/main";
    }
    @RequestMapping("/dologin")
    public String doLogin(HttpServletRequest request,String devCode,String devPassword){
       DevUser devUser =  devService.login(devCode,devPassword);
       if(devUser!=null){
            request.getSession().setAttribute("UserSession",devUser);

            return "redirect:/dev/main";
       }else{
           request.setAttribute("error","用户名或者密码错误");
           return "devlogin";
       }
    }

}
